package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.FileMapper;
import com.itheima.dao.PictureSortMapper;
import com.itheima.entity.PageResult;
import com.itheima.pojo.File;
import com.itheima.pojo.PictureSort;
import com.itheima.pojo.Todo;
import com.itheima.service.PictureSortService;
import com.itheima.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PictureSortServiceImpl implements PictureSortService {


    @Autowired
    private IdWorker idWorker;

    @Autowired
    private PictureSortMapper pictureSortMapper;

    @Autowired
    private FileMapper fileMapper;

    @Override
    @Transactional
    public void add(Map pictureSortVO) {
        //将map转换成实体类
        PictureSort pictureSort = JSON.parseObject(JSON.toJSONString(pictureSortVO), PictureSort.class);
        pictureSort.setUid(idWorker.nextId() + "");
        pictureSort.setCreateTime(new Date());
        pictureSort.setUpdateTime(new Date());
        pictureSortMapper.insertSelective(pictureSort);
    }

    @Override
    @Transactional
    public void edit(Map pictureSortVO) {
        //将map转换成实体类
        PictureSort pictureSort = JSON.parseObject(JSON.toJSONString(pictureSortVO), PictureSort.class);
        pictureSort.setUpdateTime(new Date());
        pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
    }

    @Override
    @Transactional
    public void delete(Map pictureSortVO) {
        //将map转换成实体类
        PictureSort pictureSort = JSON.parseObject(JSON.toJSONString(pictureSortVO), PictureSort.class);
        pictureSortMapper.delete(pictureSort);
    }

    @Override
    public PageResult<PictureSort> getList(Map searchMap) {
        //获取当前页
        Integer currentPage = (Integer) searchMap.get("currentPage");
        if (null == currentPage || currentPage < 0) {
            currentPage = 0;
        }
        //设置默认每页显示条数
        Integer pageSize = (Integer) searchMap.get("pageSize");
        if (null == pageSize || pageSize <= 0) {
            pageSize = 10;
        }
        PageHelper.startPage(currentPage, pageSize);
        Example example = this.createExample(searchMap);
        Page<PictureSort> pageList = (Page<PictureSort>) pictureSortMapper.selectByExample(example);
        PageResult<PictureSort> pageResult = new PageResult<>();
        List<PictureSort> pictureSortList = pageList.getResult();
        if (pictureSortList == null && pictureSortList.size() <= 0) {
            throw new RuntimeException("图片分类不存在!");
        }
        //通过图片分类uid查出该分类下面的所有图片路径
        File file = new File();
        List<String> photoList = new ArrayList<>();
        for (PictureSort pictureSort : pictureSortList) {
            String fileUid = pictureSort.getFileUid();
            file.setFileSortUid(fileUid);
            List<File> fileList = fileMapper.select(file);
            fileList.forEach(item -> photoList.add(item.getPicUrl()));
            pictureSort.setPhotoList(photoList);
        }
        pageResult.setRecords(pictureSortList);
        pageResult.setCurrent((long) pageList.getPageNum() + 1);
        pageResult.setOptimizeCountSql(true);
        pageResult.setSize((long) pageList.getPageSize());
        pageResult.setTotal(pageList.getTotal());
        pageResult.setSearchCount(true);
        return pageResult;
    }

    @Override
    public PictureSort getPictureSortByUid(Map pictureSortVO) {
        //将map转换成实体类
        PictureSort pictureSort = JSON.parseObject(JSON.toJSONString(pictureSortVO), PictureSort.class);
        pictureSort = pictureSortMapper.selectByPrimaryKey(pictureSort);
        if (pictureSort == null) {
            throw new RuntimeException("图片分类不存在!");
        }
        //todo
        //通过图片分类uid查出该分类下面的所有图片路径
        File file = new File();
        List<String> photoList = new ArrayList<>();
        String fileUid = pictureSort.getFileUid();
        file.setFileSortUid(fileUid);
        List<File> fileList = fileMapper.select(file);
        fileList.forEach(item -> photoList.add(item.getPicUrl()));
        pictureSort.setPhotoList(photoList);
        return pictureSort;
    }

    @Override
    @Transactional
    public void stick(Map pictureSortVO) {
        //将map转换成实体类
        PictureSort pictureSort = JSON.parseObject(JSON.toJSONString(pictureSortVO), PictureSort.class);
        //获取当前置顶的sort值
        Long sort = pictureSortMapper.getSort();
        pictureSort = pictureSortMapper.selectByPrimaryKey(pictureSort);
        if (pictureSort.getSort() >= sort) {
            throw new RuntimeException("该分类已经在顶端!");
        }
        //sort值越大,排序越高,给最大的sort值+1就是置顶
        pictureSort.setSort(sort + 1);
        pictureSort.setUpdateTime(new Date());
        pictureSortMapper.updateByPrimaryKeySelective(pictureSort);
    }

    /**
     * 构建查询对象
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(PictureSort.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            if (searchMap.get("uid") != null && !"".equals(searchMap.get("uid"))) {
                criteria.andLike("uid", "%" + searchMap.get("uid") + "%");
            }
            if (searchMap.get("fileUid") != null && !"".equals(searchMap.get("fileUid"))) {
                criteria.andLike("fileUid", "%" + searchMap.get("fileUid") + "%");
            }
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            if (searchMap.get("keyword") != null && !"".equals(searchMap.get("keyword"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            if (searchMap.get("parentUid") != null && !"".equals(searchMap.get("parentUid"))) {
                criteria.andLike("parentUid", "%" + searchMap.get("parentUid") + "%");
            }
            if (searchMap.get("sort") != null) {
                criteria.andEqualTo("sort", searchMap.get("sort"));
            }
            if (searchMap.get("status") != null) {
                criteria.andEqualTo("status", searchMap.get("status"));
            }
            if (searchMap.get("is_show") != null) {
                criteria.andEqualTo("is_show", searchMap.get("is_show"));
            }
            if (searchMap.get("startTime") != null) {
                criteria.andGreaterThan("createTime", searchMap.get("startTime"));
            }
        }
        return example;
    }
}
